package com.lm.ledger.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnMngt1 {

	static Connection conn = null;
	static String url = "jdbc:mysql://localhost:3306/";
	static String dbName = "dsslm";
	static String driver = "com.mysql.jdbc.Driver";
	static String userName = "root";
	static String password = "root";
	ResultSet rs;
	PreparedStatement ps;  
	
	public static Connection getConnection() throws ClassNotFoundException, SQLException{
		Class.forName(driver);
		conn = DriverManager.getConnection(url+dbName,userName,password);	
		return conn;
	}
	
	public static void closeConnection(ResultSet rs, Statement stmt, Connection conn)
	{
		try 
		{
			if(rs!=null)
				rs.close();
			if(stmt!=null)
				stmt.close();
			if(conn!=null)
				conn.close();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	public static void closeConnection(PreparedStatement pstmt, Connection conn)
	{
		try 
		{
			if(pstmt!=null)
				pstmt.close();
			if(conn!=null)
				conn.close();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}	
	public static void closeConnection(ResultSet rs, PreparedStatement pstmt, Connection conn)
	{
		try 
		{
			if(rs!=null)
				rs.close();			
			if(pstmt!=null)
				pstmt.close();
			if(conn!=null)
				conn.close();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}		
	public static void closeConnection(ResultSet rs, Statement stmt,ResultSet rs2, Statement stmt2, Connection conn)
	{
		try 
		{
			if(rs!=null)
				rs.close();
			if(stmt!=null)
				stmt.close();
			if(rs2!=null)
				rs2.close();
			if(stmt2!=null)
				stmt2.close();
			if(conn!=null)
				conn.close();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}	
	public static void closeConnection(Statement stmt, Connection conn)
	{
		try 
		{
			if(stmt!=null)
				stmt.close();
			if(conn!=null)
				conn.close();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	public static void closeConnection(Statement stmt, Statement stmt2, Statement stmt3, Connection conn)
	{
		try 
		{
			if(stmt!=null)
				stmt.close();
			if(stmt2!=null)
				stmt2.close();
			if(stmt3!=null)
				stmt3.close();			
			if(conn!=null)
				conn.close();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void saveTrxData(){
		String query = "INSERT INTO ACCOUNT (TransactionID, FormID, LocationID, Amount, Reason, ItemPurchased, InterestAmount, PersonID, Narration, TransactionType, ModeOfPayment, TransactionAuthorizerID, Status) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		//INSERT INTO ACCOUNT (TransactionID, FormID, LocationID, Amount, Reason, ItemPurchased, InterestAmount, PersonID, Narration, TransactionType, ModeOfPayment, TransactionAuthorizerID, Status) VALUES(002, 001, 001, 1000, 'pruchase', 'grain', 111, 1, 'aaa', 'debit', 'cash', 1, 'y') 
		try {
			Connection conn = getConnection();
			ps = conn.prepareStatement(query);
			
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		
	}
	
	public static void main(String[] args) {
		ConnMngt1 mngt = new ConnMngt1();
		try {
			Connection conn = mngt.getConnection();
			System.out.println(conn);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
